package com.helen.ui.newui;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.helen.adapter.TeamListAdapter2;
import com.helen.db.UserHelper;
import com.helen.entity.TeamEntity;
import com.helen.entity.TeamEntity2;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2019/1/31
 */
public class TeamActivity extends BaseActivity {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private List<TeamEntity> list = new ArrayList();
    private List<String> list2 = new ArrayList();
//    private TeamListAdapter teamListAdapter;
    private TeamListAdapter2 teamListAdapter2;
    private View errorView;
    private int page = 1;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_coupons_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("我的团队");
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvList.getParent(), false);
//        teamListAdapter = new TeamListAdapter(list);
        teamListAdapter2 = new TeamListAdapter2(list2);
//        teamListAdapter2.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        swipeRefresh.setRefreshing(true);
        swipeRefresh.setColorSchemeColors(getResources().getColor(R.color.appThemeColor));
        rvList.setLayoutManager(new LinearLayoutManager(TeamActivity.this));
        rvList.setAdapter(teamListAdapter2);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
//        teamListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                loadMore();
//            }
//        }, rvList);

        teamListAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent ti = new Intent(TeamActivity.this,TeamMemberActivity.class);
                ti.putExtra("iid",(position+1)+"");
                startActivity(ti);

            }
        });
        refresh();
    }

    @Override
    protected void updateViews() {

    }

    private void refresh() {
//        page = 1;
        getData();
    }


//    private void loadMore() {
//        page++;
//        getData();
//    }

    private void getData() {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", UserHelper.getInstance().getloginEntity(TeamActivity.this).getId());
//        params.put("page", page + "");
//        params.put("size", "10");
        EasyHttp.post(Constants.API_JSONAPI_MY_USER_SUM)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取我的团队失败==" + e.toString());
                        MToast.makeTextShort(TeamActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        MyLog.d("yang", "s===" + s);
                        if (s != null) {
                            list2.clear();
                            Gson gson = new GsonBuilder().create();
                            TeamEntity2 teamEntity2 = gson.fromJson(s, TeamEntity2.class);
                            MyLog.d("yang","teamEntity2==="+teamEntity2.getCount1());
                            list2.add(teamEntity2.getCount1());
                            list2.add(teamEntity2.getCount2());
                            list2.add(teamEntity2.getCount3());
                            list2.add(teamEntity2.getCount4());
                            list2.add(teamEntity2.getCount5());
                            list2.add(teamEntity2.getCount6());
                            list2.add(teamEntity2.getCount7());
                            list2.add(teamEntity2.getCount8());
                            list2.add(teamEntity2.getCount9());
                            list2.add(teamEntity2.getCount10());
//                            list = new Gson().fromJson(data, new TypeToken<List<TeamEntity>>() {
//                            }.getType());
//                            if (page == 1) {
//                                teamListAdapter2.getData().clear();
                                teamListAdapter2.setNewData(list2);
//                            } else {
//                                teamListAdapter.addData(list);
//                            }
//                            if (list.size() < 10) {
//                                teamListAdapter.loadMoreEnd(false);
//                                teamListAdapter.setEmptyView(errorView);
//                            } else {
//                                teamListAdapter.loadMoreComplete();
//                            }
                        }

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                        swipeRefresh.setRefreshing(false);
                    }
                });
    }
}
